<template>
  <div class="table-container">
    <div class="table-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>文章管理</el-breadcrumb-item>
        <el-breadcrumb-item>分类列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="table-content">
      <div class="table-bar">
        <el-button type="primary" @click="handleAdd">新增分类</el-button>
      </div>
      <el-table
        empty-text="当前数据空空如也"
        :data="types"
        tooltip-effect="dark"
        style="width: 100%"
        row-key="id"
        border
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="id" label="ID" width="200"></el-table-column>
        <el-table-column prop="sort" label="排序" width="100"></el-table-column>
        <el-table-column prop="cover" label="LOGO">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.cover"
              style="width: 100px; height: 100px"
              :src="scope.row.cover"
              :preview-src-list="[scope.row.cover]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="type_name" label="名称"></el-table-column>
        <el-table-column label="操作" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              style="margin-right: 5px"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @confirm="handleDelete(scope.index,scope.row)"
            >
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-nav">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        page: 1,
        pageSize: 10,
        total: 0,
        keyword: '',
        types: [],
        multipleSelection: [],
      }
    },
    mounted() {
      this.queryList();
    },
    methods: {
      handleAdd() {
        this.$router.push({name: 'addArticleType'})
      },
      handleEdit(index, row) {
        const id = row.id;
        this.$router.push({name: 'editArticleType', query: {id}});
      },
      handleDelete(index, row) {
        this.toDelete(row.id);
      },
      handleSizeChange(val) {
        this.types = [];
        this.pageSize = val;
        this.queryList()
      },
      handleCurrentChange(val) {
        this.types = [];
        this.page = val;
        this.queryList();
      },
      queryList() {
        this.$http.get('/api/root/ArticleType/index', {
          page: this.page,
          pagesize: this.pageSize,
          keyword: this.keyword
        }).then(rest => {
          if (rest.data) {
            this.types = rest.data.types;
            this.total = rest.data.count;
          }
        })
      },
      toDelete(id) {
        const _ths = this;
        this.$http.get('/api/root/ArticleType/del', {
          id: id,
        }).then(rest => {
          if (rest.msg === 'ok') {
            this.$message.success('删除成功');
            setTimeout(function () {
              _ths.queryList();
            }, 600)
          } else {
            this.$message.error(rest.msg);
          }
        })
      }
      ,
      loadChildren(tree,treeNode,resolve) {
        console.log(tree,treeNode,resolve);
      }
    }

  }
</script>
<style lang="less" scoped>
  @import 'index.less';
</style>
